import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xws/utils/theme_util.dart';

/// app 按钮
class AppBtn extends StatelessWidget {
  const AppBtn({
    Key? key,
    this.text,
    this.child,
    this.onTap,
    this.padding,
    this.width,
    this.height,
    this.color,
    this.border,
    this.borderRadius,
    this.alignment,
    this.disabledColor,
    this.textStyle,
    this.radius,
    this.gradient,
    this.onLongPress,
    this.clipBehavior = Clip.none,
    this.hasAnimation = false,
    this.curve = Curves.ease,
    this.duration = const Duration(milliseconds: 300),
  }) : super(key: key);

  const AppBtn.center({
    Key? key,
    this.text,
    this.child,
    this.onTap,
    this.padding,
    this.width,
    this.height,
    this.color,
    this.border,
    this.borderRadius,
    this.alignment = Alignment.center,
    this.disabledColor,
    this.textStyle,
    this.radius,
    this.gradient,
    this.onLongPress,
    this.clipBehavior = Clip.none,
    this.hasAnimation = false,
    this.curve = Curves.ease,
    this.duration = const Duration(milliseconds: 300),
  }) : super(key: key);

  const AppBtn.animated({
    Key? key,
    this.text,
    this.child,
    this.onTap,
    this.padding,
    this.width,
    this.height,
    this.color,
    this.border,
    this.borderRadius,
    this.alignment = Alignment.center,
    this.disabledColor,
    this.textStyle,
    this.radius,
    this.gradient,
    this.onLongPress,
    this.clipBehavior = Clip.none,
    this.hasAnimation = true,
    this.curve = Curves.ease,
    this.duration = const Duration(milliseconds: 300),
  }) : super(key: key);

  const AppBtn.noPadding({
    Key? key,
    this.text,
    this.child,
    this.onTap,
    this.padding = EdgeInsets.zero,
    this.width,
    this.height,
    this.color,
    this.border,
    this.borderRadius,
    this.alignment,
    this.disabledColor,
    this.textStyle,
    this.radius,
    this.gradient,
    this.onLongPress,
    this.clipBehavior = Clip.none,
    this.hasAnimation = false,
    this.curve = Curves.ease,
    this.duration = const Duration(milliseconds: 300),
  }) : super(key: key);

  final String? text;
  final TextStyle? textStyle;
  final Widget? child;
  final Function()? onTap;
  final Function()? onLongPress;
  final EdgeInsetsGeometry? padding;
  final double? width;
  final double? height;
  final Color? color;
  final Color? disabledColor;
  final BoxBorder? border;
  final BorderRadiusGeometry? borderRadius;
  final AlignmentGeometry? alignment;

  final double? radius;

  final Gradient? gradient;

  final Clip clipBehavior;

  final bool hasAnimation;

  final Curve curve;

  final Duration duration;

  @override
  Widget build(BuildContext context) {
    Widget box = hasAnimation
        ? AnimatedContainer(
            duration: duration,
            curve: curve,
            width: width,
            height: height,
            padding: padding ?? const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
            alignment: alignment,
            clipBehavior: clipBehavior,
            child: child ??
                Text(
                  text ?? '',
                  style: textStyle ?? TextStyle(color: white(context)),
                ),
            decoration: BoxDecoration(
              color: _color(context),
              border: border,
              borderRadius: borderRadius ?? BorderRadius.all(Radius.circular(radius ?? 4)),
              gradient: gradient,
            ),
          )
        : Container(
            width: width,
            height: height,
            padding: padding ?? const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
            alignment: alignment,
            clipBehavior: clipBehavior,
            child: child ??
                Text(
                  text ?? '',
                  style: textStyle ?? TextStyle(color: white(context)),
                ),
            decoration: BoxDecoration(
              color: _color(context),
              border: border,
              borderRadius: borderRadius ?? BorderRadius.all(Radius.circular(radius ?? 4)),
              gradient: gradient,
            ),
          );

    if (onTap != null || onLongPress != null) {
      box = GestureDetector(
        child: box,
        onTap: onTap,
        onLongPress: onLongPress == null
            ? null
            : () {
                //震动
                HapticFeedback.vibrate();
                onLongPress?.call();
              },
      );
    }

    return box;
  }

  Color _color(BuildContext context) {
    if (onTap == null) {
      if (color == Colors.transparent) return color!;
      return disabledColor ?? (color ?? theme(context)).withOpacity(0.2);
    }

    return color ?? theme(context);
  }
}
